﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using HHControls.CreditEducation;
using IGM.Presentation.Assignment;
using IGM.Presentation.Help;
using IGM.Presentation.Management;
using IGM.Presentation.Users;

namespace IGM.Presentation
{
    #region Navigation

    public class Navigation
    {
        public const int ItemWidth = 150;
        public const int ItemHeight = 30;
        public const int StartX = 20;
        public const int StartY = 39;

        public static List<NavGroup> NavGroups = new List<NavGroup>();
        public static List<NavItem> NavItems = new List<NavItem>();

        public static int IndexOfItem(string name)
        {
            var item = NavItems.First(i => i.Name == name);
            return NavItems.IndexOf(item);
        }

        public static int IndexOfGroup(string name)
        {
            var @group = NavGroups.First(i => i.Name == name);
            return NavGroups.IndexOf(@group);
        }

        public static void Initialize()
        {
            NavItems.Clear();
            NavGroups.Clear();

            NavItems.Add(new NavItem { Name = "Hướng dẫn sử dụng", GroupName = "expHelp", ShowingControl = new HelpControl() });
            NavItems.Add(new NavItem { Name = "Thông tin tác giả", GroupName = "expHelp", ShowingControl = new About() });

            NavItems.Add(new NavItem { Name = "Quản lý Thực tập", GroupName = "expManagement", ShowingControl = new InternManagementControl() });
            NavItems.Add(new NavItem { Name = "Quản lý Điểm", GroupName = "expManagement", ShowingControl = new StudentMarksControl() });
            NavItems.Add(new NavItem { Name = "Quản lý Sinh viên", GroupName = "expManagement", ShowingControl = new StudentsControl() });
            NavItems.Add(new NavItem { Name = "Quản lý Giảng viên", GroupName = "expManagement", ShowingControl = new LecturersControl() });
            NavItems.Add(new NavItem { Name = "Quản lý TT khác", GroupName = "expManagement", ShowingControl = new OtherControl() });

            NavItems.Add(new NavItem { Name = "Phân công thực tập", GroupName = "expAssignment", ShowingControl = new GroupLecturersControl() });
            NavItems.Add(new NavItem { Name = "Phân công tốt nghiệp", GroupName = "expAssignment", ShowingControl = new StudentLecturersControl() });
            NavItems.Add(new NavItem { Name = "Hội đồng thực tập", GroupName = "expAssignment", ShowingControl = new SubcommitteeGroupsControl() });
            NavItems.Add(new NavItem { Name = "Hội đồng tốt nghiệp", GroupName = "expAssignment", ShowingControl = new SubcommitteeStudentsControl() });

            NavItems.Add(new NavItem { Name = "Quản lý người dùng", GroupName = "expUsers", ShowingControl = new UsersControl() });

            NavGroups.Add(new NavGroup { Text = "Hướng dẫn", Name = "expHelp" });
            NavGroups.Add(new NavGroup { Text = "Phân công", Name = "expAssignment" });
            NavGroups.Add(new NavGroup { Text = "Quản lý", Name = "expManagement" });
            NavGroups.Add(new NavGroup { Text = "Người dùng", Name = "expUsers" });
            NavGroups.Add(new NavGroup { Text = "CT chính", Name = "expMain" });            
        }

        public void ShowMenu(int i)
        {
            switch (i)
            {
                case 0:
                    NavGroups.Add(new NavGroup { Text = "Hướng dẫn", Name = "expHelp" });
                    NavGroups.Add(new NavGroup { Text = "Người dùng", Name = "expUsers" });
                    NavGroups.Add(new NavGroup { Text = "CT chính", Name = "expMain" });
                    break;
                case 1:
                    NavGroups.Add(new NavGroup { Text = "Hướng dẫn", Name = "expHelp" });
                    NavGroups.Add(new NavGroup { Text = "Phân công", Name = "expAssignment" });
                    NavGroups.Add(new NavGroup { Text = "CT chính", Name = "expMain" });
                    break;
                case 2:
                    NavGroups.Add(new NavGroup { Text = "Hướng dẫn", Name = "expHelp" });
                    NavGroups.Add(new NavGroup { Text = "Quản lý", Name = "expManagement" });
                    NavGroups.Add(new NavGroup { Text = "CT chính", Name = "expMain" });
                    break;
                default:
                    NavGroups.Add(new NavGroup { Text = "Hướng dẫn", Name = "expHelp" });
                    NavGroups.Add(new NavGroup { Text = "CT chính", Name = "expMain" });
                    break;
            }
        }

        private static void AddItemToGroup()
        {
            var startPoint = new Point(StartX, StartY);

            foreach (var navGroup in NavGroups)
            {
                var currentGroupName = navGroup.Name;
                foreach (var navItem in NavItems.Where(n => n.GroupName == currentGroupName))
                {
                    navGroup.Instance.Controls.Add(navItem.Instance);
                    navItem.Instance.Location = new Point(startPoint.X, startPoint.Y + (ItemHeight + 1) * (navGroup.Instance.Controls.Count - 1));
                }

                navGroup.Instance.ExpandedSize = startPoint.Y + (ItemHeight + 1) * navGroup.Instance.Controls.Count;
            }
        }

        private static void AddGroupToControlCollection(Control.ControlCollection controlCollection)
        {
            foreach (var navItem in NavItems)
            {
                var item = navItem;
                navItem.Instance.Click += (s, e) => ShowControlOnPanel(item);
            }

            controlCollection.Clear();
            foreach (var navGroup in NavGroups)
            {
                controlCollection.Add(navGroup.Instance);

                var currentGroup = navGroup;
                navGroup.Instance.Click += (s, e) => ExpandGroup(currentGroup);
                navGroup.Instance.MouseHover += (s, e) => ExpandGroup(currentGroup);
            }
        }

        public static void ExpandGroup(NavGroup currentGroup)
        {
            foreach (var otherGroup in NavGroups.Where(otherGroup => otherGroup.Name != currentGroup.Name))
            {
                otherGroup.Instance.IsExpand = false;
            }

            if (!currentGroup.Instance.IsExpand)
            {
                currentGroup.Instance.IsExpand = true;
            }
        }

        public static void ShowControlOnPanel(NavItem item)
        {
            if (item.ShowingControl == null) return;

            if (item.ShowingControl is Form)
            {
                if (item.ShowingControl.IsDisposed)
                {
                    item.ShowingControl = (Form)Activator.CreateInstance(item.ShowingControl.GetType());
                }
                ((Form)item.ShowingControl).ShowDialog();
            }
            else if (item.ShowingControl is UserControl)
            {
                var navItem = NavItems.FirstOrDefault(i => i.Instance.IsClicked);
                if (navItem != null)
                {
                    navItem.Instance.IsClicked = false;
                }
                item.Instance.IsClicked = true;

                item.ShowingControl = (Control)Activator.CreateInstance(item.ShowingControl.GetType());

                Main.Main.Instance.AddUserControlToPanel(item.ShowingControl);
            }
        }

        public static void Show(Control.ControlCollection controlCollection)
        {
            AddItemToGroup();
            AddGroupToControlCollection(controlCollection);
        }
    }

    #endregion

    #region Nest Classes

    public class NavGroup
    {
        public NavGroup()
        {
            Instance = new HHArrowExpand { Dock = DockStyle.Top, IsExpand = false };
        }

        public HHArrowExpand Instance { get; private set; }
        public string Text
        {
            get { return Instance.TextTitle; }
            set { Instance.TextTitle = value; }
        }

        public string Name { get; set; }
    }

    public class NavItem
    {
        public NavItem()
        {
            Instance = new HHSimpleButton
            {
                Width = Navigation.ItemWidth,
                Height = Navigation.ItemHeight,
                Font = new Font("Tahoma", 10)
            };
        }

        public HHSimpleButton Instance { get; private set; }
        public string Name
        {
            get { return Instance.Text; }
            set { Instance.Text = value; }
        }
        public string GroupName { get; set; }
        public Control ShowingControl { get; set; }
    }

    #endregion
}
